www.gusucode.com > VC++网络版的打字软件源程序-源码程序 > VC++网络版的打字软件源程序-源码程序\code\TypeSrv V2.0\TypeSrv.cpp

    //Download by http://www.NewXing.com
// TypeSrv.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "TypeSrv.h"

#include "MainFrm.h"
#include "TypeSrvDoc.h"
#include "MainView.h"

#include "SocketInfo.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include"SkinPlusPlus.h"
#include <odbcinst.h>	//ODBC数据库API头文件
#include "MainView.h"
#include "Crypt.h"		//加密解密头文件
#include "RegisterDlg.h"

#define VOLUME_KEY	46

/////////////////////////////////////////////////////////////////////////////
// CTypeSrvApp

BEGIN_MESSAGE_MAP(CTypeSrvApp, CWinApp)
	//{{AFX_MSG_MAP(CTypeSrvApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_REGISTER_SOFT, OnRegisterSoft)
	ON_UPDATE_COMMAND_UI(ID_REGISTER_SOFT, OnUpdateRegisterSoft)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTypeSrvApp construction

CTypeSrvApp::CTypeSrvApp()
{
	m_bRegister			= FALSE;
	m_strSN				= _T("");
	m_strCode			= _T("");
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CTypeSrvApp object

CTypeSrvApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CTypeSrvApp initialization

BOOL CTypeSrvApp::InitInstance()
{
	AfxEnableControlContainer();

	if(!AfxSocketInit())
	{
		AfxMessageBox("初始化套接字失败!");
		return FALSE;
	}


	if(!CreateDataSource())
	{
		AfxMessageBox("创建数据源失败!");
		return FALSE;
	}

	m_bRegister=CheckRegister();//检查是否已注册

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	InitializeSkin("skin//XPCorona.ssk");

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CTypeSrvDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CMainView));
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The one and only window has been initialized, so show and update it.
	m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
	m_pMainWnd->UpdateWindow();

	if(m_bRegister==FALSE)
	{
		CString str="您还没有注册本软件,网络测试时间只能用10分钟,要注册吗?";
		if(MessageBox(m_pMainWnd->m_hWnd,str,"提示信息",MB_YESNO | MB_ICONQUESTION)==IDYES)
		{
			CRegisterDlg dlg;
			GetCode(m_strCode);
			dlg.m_strCode=m_strCode;
			dlg.m_strSN=m_strSN;
			dlg.m_bRegister=m_bRegister;
			if(dlg.DoModal()==IDOK)
			{
				if(RegisterSofts(dlg.m_strSN)==TRUE)
				{
					str="注册成功!谢谢您注册本软件。";
					m_bRegister=TRUE;
				}
				else
				{
					str="注册失败!您填写的注册码有误。";
					m_bRegister=FALSE;
				}
				MessageBox(m_pMainWnd->m_hWnd,str,"提示信息",MB_OK | MB_ICONINFORMATION);
			}
		}
	}

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CString	m_strHostName;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	virtual BOOL OnInitDialog();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	m_strHostName = _T("");
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Text(pDX, IDC_HOST_NAME, m_strHostName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CTypeSrvApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CTypeSrvApp message handlers


int CTypeSrvApp::ExitInstance() 
{
	// TODO: Add your specialized code here and/or call the base class
	ExitSkin();
	return CWinApp::ExitInstance();
}

BOOL CTypeSrvApp::CreateDataSource()
{
	//创建数据源

	BOOL bReturn;
	char szPathName[256];
	char szAttr[256];
	GetModuleFileName(NULL,szPathName,256);
	CString str=szPathName;
	int nPos=str.ReverseFind('\\');
	CString strPath=str.Left(nPos);
	
	sprintf(szAttr,	
		"DSN=TypeData#"
		"Description=打字测试数据库#"
        "FileType=Access#"
        "DBQ=%s\\Data\\TypeData.mdb#",
		strPath);

	for(int i=0;i<256;i++)
	{
		if(szAttr[i]=='#')
			szAttr[i]='\0';
	}

	/*
	bReturn=SQLConfigDataSource(NULL,ODBC_ADD_DSN,
		"Microsoft Access Driver (*.mdb)",
		"DSN=TypeData\0"
		"Description=打字测试数据库\0"
        "FileType=Access\0"
        "DBQ=.\\Data\\TypeData.mdb\0");
		*/
	bReturn=SQLConfigDataSource(NULL,ODBC_ADD_DSN,
		"Microsoft Access Driver (*.mdb)",
		(LPCSTR)szAttr);

	return bReturn;
}

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CString strHostName=CSocketInfo::GetLocalHostName();
	CString strHostAddress=CSocketInfo::GetLocalHostIP();
	UpdateData(TRUE);
	m_strHostName.Format("服务器名称:%s 或 %s",strHostName,strHostAddress);
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CTypeSrvApp::RegisterSofts(CString strSN)
{
	HKEY	hKey;
	LONG	lRet;
	TCHAR	szBuffer[256];
	TCHAR	szSN[256];
	DWORD	dwLength;
	DWORD	dwDataType;
	CString	strSoft="Software";
	CString strSub="TypeSrv";

	/*打开注册表*/
	lRet=::RegOpenKeyEx(HKEY_CURRENT_USER,LPCTSTR(strSoft),NULL,KEY_READ | KEY_WRITE,&hKey);
	if(lRet!=ERROR_SUCCESS)
	{
		AfxMessageBox("打开注册表有误",MB_OK | MB_ICONERROR);
		THROW(new CException(TRUE));
		return FALSE;
	}

	/*查找注册表子项*/
	lRet=::RegQueryValueEx(hKey,strSub,NULL,&dwDataType,(LPBYTE)szBuffer,&dwLength);
	strcpy(szSN,strSN);
	if(lRet!=ERROR_SUCCESS)
	{
		//找不到,保存系列号SN
		RegSetValueEx(hKey,strSub,0,REG_SZ,(LPBYTE)szSN,strlen(szSN));
	}
	else
	{
		//找到,但值与现在不一样,删除并重新保存系列号SN
		if(strcmp(szSN,szBuffer))
		{
			RegDeleteValue(hKey,strSub);
			RegSetValueEx(hKey,strSub,0,REG_SZ,(LPBYTE)szSN,strlen(szSN));
		}
	}

	/*关闭注册表*/
	RegCloseKey(hKey);

	/////////////////////////////////////////////////////////
	/*解密*/
	CString strCode=CCrypt::Decrypt(strSN);
	
	GetCode(m_strCode);

	if(strCode==m_strCode)
	{
		m_bRegister=TRUE;
		m_strSN=strSN;
	}
	else
	{
		m_bRegister=FALSE;
		m_strSN="还没注册";
	}

	return m_bRegister;
}

BOOL CTypeSrvApp::CheckRegister()
{
	HKEY	hKey;
	LONG	lRet;
	TCHAR	szSN[256];
	DWORD	dwLength;
	DWORD	dwDataType;
	CString	strSoft="Software";
	CString strSub="TypeSrv";

	/*打开注册表*/
	lRet=::RegOpenKeyEx(HKEY_CURRENT_USER,LPCTSTR(strSoft),NULL,KEY_READ | KEY_WRITE,&hKey);
	if(lRet!=ERROR_SUCCESS)
	{
		AfxMessageBox("打开注册表有误",MB_OK | MB_ICONERROR);
		THROW(new CException(TRUE));
		return FALSE;
	}

	/*查找注册表子项*/
	lRet=::RegQueryValueEx(hKey,strSub,NULL,&dwDataType,(LPBYTE)szSN,&dwLength);
	if(lRet!=ERROR_SUCCESS)
	{
		return FALSE;
	}
	else
	{
		m_strSN=szSN;//保存系列号
	}

	/*关闭注册表*/
	RegCloseKey(hKey);

	////////////////////////////////////////////////////////
	GetCode(m_strCode);//硬盘系列号
	
	CString strSN=CCrypt::Encrypt(m_strCode);

	return (strSN==m_strSN);
}

void CTypeSrvApp::OnRegisterSoft() 
{
	// TODO: Add your command handler code here
	CString str;
	CRegisterDlg dlg;
	GetCode(m_strCode);
	dlg.m_strCode=m_strCode;
	dlg.m_strSN=m_strSN;
	dlg.m_bRegister=m_bRegister;
	if(dlg.DoModal()==IDOK)
	{
		if(RegisterSofts(dlg.m_strSN)==TRUE)
		{
			str="注册成功!谢谢您注册本软件。";
		}
		else
		{
			str="注册失败!您填写的注册码有误。";
		}
		MessageBox(m_pMainWnd->m_hWnd,str,"提示信息",MB_OK | MB_ICONINFORMATION);
	}
}

void CTypeSrvApp::OnUpdateRegisterSoft(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_bRegister);
}



void CTypeSrvApp::GetCode(CString& str)
{
	CString strRootPathName="c:\\";
	char	szVolumeNameBuff[1024];
	DWORD	nVolumeNameSize=1024;
	DWORD	nVolumeSerialNumber=1024;
	DWORD	nMaximumComponentLength;
	DWORD	nFileSystemFlags;
	char	szFileSystemNameBuffer[1024];
	DWORD	nFileSystemNameSize=1024;

	GetVolumeInformation(strRootPathName,szVolumeNameBuff,
		nVolumeNameSize,&nVolumeSerialNumber,
		&nMaximumComponentLength,
		&nFileSystemFlags,szFileSystemNameBuffer,nFileSystemNameSize);

	str.Format("%d",nVolumeSerialNumber);//格式化硬盘

	str=CCrypt::Encrypt(str,VOLUME_KEY);

}